//package com.liqing.movecomment.elastic;
//
//import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
//import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
//import com.fasterxml.jackson.core.JsonProcessingException;
//import com.liqing.movecomment.dao.MoveDao;
//import com.liqing.movecomment.elastic.entity.MoveElas;
//import com.liqing.movecomment.entity.Move;
//import com.liqing.movecomment.service.MoveService;
//import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
//import org.elasticsearch.action.support.master.AcknowledgedResponse;
//import org.springframework.stereotype.Component;
//import com.fasterxml.jackson.databind.ObjectMapper;
//import org.elasticsearch.action.bulk.BulkRequest;
//import org.elasticsearch.action.bulk.BulkResponse;
//import org.elasticsearch.action.index.IndexRequest;
//import org.elasticsearch.client.RequestOptions;
//import org.elasticsearch.client.RestHighLevelClient;
//import org.elasticsearch.client.indices.CreateIndexRequest;
//import org.elasticsearch.client.indices.CreateIndexResponse;
//import org.elasticsearch.client.indices.GetIndexRequest;
//import org.elasticsearch.common.settings.Settings;
//import org.elasticsearch.common.xcontent.XContentBuilder;
//import org.elasticsearch.common.xcontent.XContentType;
//import org.elasticsearch.common.xcontent.json.JsonXContent;
//import org.elasticsearch.action.search.SearchRequest;
//import org.elasticsearch.action.search.SearchResponse;
//import org.elasticsearch.client.RequestOptions;
//import org.elasticsearch.client.RestHighLevelClient;
//import org.elasticsearch.index.query.Operator;
//import org.elasticsearch.index.query.QueryBuilder;
//import org.elasticsearch.index.query.QueryBuilders;
//import org.elasticsearch.search.SearchHit;
//import org.elasticsearch.search.builder.SearchSourceBuilder;
//
//import javax.annotation.PostConstruct;
//import javax.annotation.Resource;
//import java.io.IOException;
//import javax.annotation.Resource;
//import java.io.IOException;
//import java.util.ArrayList;
//import java.util.Date;
//import javax.annotation.Resource;
//import java.util.List;
//import java.util.Map;
//
//import static org.elasticsearch.client.RequestOptions.DEFAULT;
//
///**
// * @author 黄靖
// * @author 2023-05-21 16:25
// * description:
// */
//@Component
//public class LoadElas implements LoadElasService {
//
//    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(LoadElas.class);
//    //</editor-fold>
//    // 索引名字
//    private static final String INDEX = "move-index";
//    @Resource
//    RestHighLevelClient client;
//    @Resource
//    private MoveDao moveDao;
//
//
//    @Override
//    @PostConstruct
//    public void loadEs() throws IOException {
//        GetIndexRequest request = new GetIndexRequest(INDEX);
//        //2. 通过client去操作
//        //  boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
////        if (exists) {
////            //往索引里面填充数据
////
////            insertDocument();
////        } else {
//        //删除索引
//        deleteIndex();
//        //不存在创建索引
//        createIndex();
//        //往索引里面填充数据
//        insertDocument();
//
//        //   }
//    }
//
//    public void deleteIndex() throws IOException {
//        //1. 准备request对象
//
//        DeleteIndexRequest request = new DeleteIndexRequest(INDEX);
//        //2. 通过client对象执行
//        AcknowledgedResponse delete = client.indices().delete(request, RequestOptions.DEFAULT);
//        //3. 获取返回结果
//        log.info("ES 删除成功");
//    }
//
//    @Override
//    public void createIndex() {
//        // settings mappings
//        Settings settings = Settings.builder().put("number_of_replicas", "1").put("number_of_shards", "5").build();
//        XContentBuilder mappings = null;
//        try {
//            mappings = JsonXContent.contentBuilder().startObject().startObject("properties").startObject("id").field("type", "integer").endObject().startObject("name").field("type", "text").endObject().endObject().endObject();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        // 构造创建索引的请求
//        CreateIndexRequest request = new CreateIndexRequest(INDEX).settings(settings).mapping(mappings);
//        // 使用客户端发送请求
//        CreateIndexResponse response = null;
//        try {
//            response = client.indices().create(request, DEFAULT);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        if (!ObjectUtils.isEmpty(response)) {
//            log.info("创建索引成功");
//        }
//    }
//
//    public void insertDocument() throws IOException {
//        ObjectMapper mapper = new ObjectMapper();
//        BulkRequest request = new BulkRequest();
//        final LambdaQueryWrapper<Move> wrapper = new LambdaQueryWrapper<>();
//        List<Move> moves = moveDao.selectList(null);
//
//
//        if (moves.size() == 0) {
//            return;
//        }
//
//
//        moves.stream().parallel().forEach(move -> {
//            MoveElas moveElas = new MoveElas(move.getId(), move.getName());
//            try {
//                request.add(new IndexRequest(INDEX).id(moveElas.getId().toString()).source(mapper.writeValueAsString(moveElas), XContentType.JSON));
//            } catch (JsonProcessingException e) {
//                e.printStackTrace();
//            }
//        });
//        BulkResponse response = client.bulk(request, DEFAULT);
//        if (ObjectUtils.isNotEmpty(response)) {
//            log.info("数据插入成功");
//        }
//    }
//
//    @Override
//    public List<Integer> testMatch(String keyWord) throws IOException {
//        // 1、创建查询请求的对象
//        SearchRequest searchRequest = new SearchRequest(INDEX);
//        // 2、查询条件构造器
//        SearchSourceBuilder builder = new SearchSourceBuilder();
//        // 3、设置查询条件
//        // ------------
//        builder.query(QueryBuilders.matchQuery("name", keyWord));
//        // ------------
//        log.info("执行的查询是==》{}", keyWord);
//        searchRequest.source(builder);
//        // 4、执行查询
//        SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
//        // 5、获取查询结果
//        SearchHit[] hits = response.getHits().getHits();
//        List<Integer> list = new ArrayList<>();
//        for (SearchHit hit : hits) {
//            Map<String, Object> map = hit.getSourceAsMap();
//            Integer id = (Integer) map.get("id");
//            log.info("{}", map);
//            list.add(id);
//        }
//        return list;
//    }
//}
